package com.test.controller;


import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.R;
import com.test.adapter.GridViewAdapter;
import com.test.entity.Icon;
import com.test.util.ImgUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 表格形式数据展示
 *
 * @Author Musique
 * 2020/8/5
 */
public class GridViewActivity extends AppCompatActivity {

    private Context mContext;
    private GridView mGridView;
    private GridViewAdapter mGridViewAdapter;
    private List<Icon> mIconList = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_demo);
        mContext = this;
        gridViewService();
    }

    /**
     * 协议个启动业务逻辑
     */
    public void gridViewService() {
        mGridView = findViewById(R.id.gridView);

        mIconList = new ArrayList<>();
        // 开始播放（线程）
        new Thread() {
            @Override
            public synchronized void run() {
                try {
                    for (int i = 1; i < 15; i++) {
                        Icon mIcon = new Icon();
                        String img = "iv_lol_icon" + i;
                        // mIcon.setAIcon(R.mipmap.iv_lol_icon1);
                        //ImgUtil.getResourceByReflect(img)这是获取图片id的工具类
                        mIcon.setAIcon(ImgUtil.getResourceByReflect(img));
                        mIcon.setTitle("图片" + i);
                        mIconList.add(mIcon);
                    }
                } catch (Exception e) {
                    System.out.println("意外停止");
                }
            }
        }.start();
        System.out.println("mIconList:" + mIconList);
        mGridViewAdapter = new GridViewAdapter(mIconList, mContext);
        mGridView.setAdapter(mGridViewAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int m = i + 1;
                Toast.makeText(mContext, "点击第" + m + " 图标", Toast.LENGTH_LONG).show();
            }
        });
    }
}
